clear all
load('F:\TEST\CPM-new\output\output-no15\mask_intersubnetwork.mat')
%MASK�ļ�����MASK.MAT

path1      = spm_select(1,'dir','please data1 dir');
%����spm_select����ѡȡ���������ļ���
file1         = dir([path1,filesep, '*.mat']);
%�������fc������ļ���
%ע�⣬ע�⣬ÿ�����Ե����ݱ�����mat��ʽ��
%������Ǵ򿪺�ֻ��һ������Z�����ݵĻ�����û���������ݡ�

for i=1:length(file1) 
    load([path1,filesep,file1(i).name]);%�������
    Z=Z.*mask;%�Ƿ�����MASK
    %Z=triu(Z,1);
    Z(isnan(Z)) = 0;%��NaNɾ��
    Z(Z==Inf)    = 0;%��������ɾ��
    Z(Z==-Inf)   = 0;%�Ѹ�����ɾ��
    Z=Z(:);
    all_mat(i,:)=Z;%
    clear Z  %��Z���     
end  
%�����һ�鱻�ԡ�
all_mat=sum(all_mat,2);
%�Ա����������

load Switch_cost.txt; 
all_behav = Switch_cost(:,4);
%�����
permutation=1;
%�û�����
sub_no=length(all_mat);
%��������
h=waitbar(0,'wait')
%������
for per=1:permutation
    if per==1
        all_behav=all_behav;
    else
        all_behav=all_behav(randperm(sub_no));
    end
for i=1:sub_no
  test_data=all_mat(i);
  %�������Լ�
  train_data=all_mat;
  train_data(i)=[];
  train_label=all_behav;
  train_label(i)=[];
  %����ѵ����
  b=robustfit(train_data,train_label);
  %�ع�õ�ϵ��
  predict_label(i,:)=b(1)+b(2)*test_data;
  %Ԥ��Ľ��
end
corr_index(per)=corr(all_behav,predict_label);
waitbar(per/permutation,h,[num2str(per),'/',num2str(permutation)]); 
end
close(h);
p_value = mean(corr_index >= corr_index(1));

fprintf('\n the corr is %d',corr_index(1)) 
fprintf('\n the p value is %d',p_value) 
  